package com.bulletelc.apollopre.module.main.center.orders.refunk;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;
import com.github.nukc.amountview.AmountView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by CX on 2018/1/28.
 * 退货/退款申请界面
 */

public class RefunkActivity extends MVPBaseActivity<RefunkContract.View, RefunkPresenter> implements RefunkContract.View
, View.OnClickListener, FoolBar.ClickCallback{

    // views
    private FoolBar flBar;
    private RadioGroup rgType;
    private AmountView avQuantity;
    private TextView tvReasonShow;
    private EditText etLeaveReason;
    private LinearLayout llChooseReason;
    private Button btnCommit;
    private int reasonType;
    private int applyType;
    private int quantity;

    // data
    private boolean reasonChosen = false;
    private boolean refunkType = false;
    private String orderId;
    private String orderItemId;


    @Override
    public int getLayoutRes() {
        return R.layout.refunk_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        rgType = findView(R.id.rg_type);
        avQuantity = findView(R.id.av_quantity);
        tvReasonShow = findView(R.id.tv_reason_show);
        llChooseReason = findView(R.id.ll_choose_reason);
        etLeaveReason = findView(R.id.et_leave_reason);
        btnCommit = findView(R.id.btn_refunk_commit);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        flBar.setTitle(this.getResources().getString(R.string.order_service_apply));
        btnCommit.setOnClickListener(this);
        llChooseReason.setOnClickListener(this);
        rgType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id) {
                    case R.id.rb_exchange:
                        applyType = 20;
                        break;
                    case R.id.rb_both:
                        applyType = 30;
                        break;
                }
                refunkType = true;
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            quantity = 1;
            orderId = intent.getStringExtra(IntentCode.EXTRA_KEY_ORDER_ID);
            orderItemId = intent.getStringExtra(IntentCode.EXTRA_KEY_ORDER_ITEM_ID);
            int maxCount = intent.getIntExtra(IntentCode.EXTRA_KEY_ORDER_QUANTITY, 0);
            avQuantity.setGoods_storage(maxCount);
            avQuantity.setListener(new AmountView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(View view, int amount) {
                    quantity = amount;
                }
            });
        }
    }

    @Override
    protected RefunkPresenter createPresenter() {
        return new RefunkPresenter();
    }

    @Override
    public void onSuccessApply() {
        Toast.makeText(this, "提交申请成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onFailApply(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorApply(String msg) {
        ToastUtil.showLongToast(this,R.string.network_msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_choose_reason:
                reasonChoose();
                break;

            case R.id.btn_refunk_commit:
                if (getCommitJson() != null) {
                    presenter.refunkApply(getCommitJson());
                }
                break;
        }

    }

    /**
     *
     */
    private void reasonChoose() {

        final String[] reasons = {
                "质量问题",
                "漏发或产品损坏",
                "实物与描述不相符",
                "其他原因",
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("退款理由")
                .setSingleChoiceItems(reasons, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        tvReasonShow.setText(reasons[i]);
                        switch (i) {
                            case 0:
                                reasonType = 10;
                                break;
                            case 1:
                                reasonType = 20;
                                break;
                            case 2:
                                reasonType = 30;
                                break;
                            case 3:
                                reasonType = 40;
                                break;
                        }
                        reasonChosen = true;
                        dialogInterface.dismiss();
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 获取提交的json参数
     * @return
     */
    private String getCommitJson() {
        String json = null;
        if (!refunkType) {
            Toast.makeText(this,"请选择退款类型",Toast.LENGTH_SHORT).show();
            return json;
        }
        if (!reasonChosen) {
            Toast.makeText(this,"请选退款理由",Toast.LENGTH_SHORT).show();
            return json;
        }
        JSONObject obj = new JSONObject();
        JSONObject objSub = new JSONObject();
        try {
            objSub.put("quantity", quantity); //退货数量
            objSub.put("sales_order_item_id", orderItemId);//订单item Id
            objSub.put("sales_order_id", orderId);//订单id
            objSub.put("apply_type", applyType); //退款类型(10/20/30)
            objSub.put("reason_type", reasonType); //退款原因类型
            objSub.put("reason", etLeaveReason.getText().toString()); //退款原因说明
            obj.put("refundOrder", objSub);
            json = obj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }
}
